package com.qf.dao.impl;

import com.qf.dao.ProjectDao;
import com.qf.pojo.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName ProjectDaoImpl
 * @Description TODO
 * @Author 86181
 * @Date 2022/9/2 14:13
 * @Version 1.0
 **/
@Repository("projectDao")
public class ProjectDaoImpl implements ProjectDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public int addProject(Project project) {
        int update = jdbcTemplate.update("insert into project (pname,starttime,endtime,did,cid,updatetime,status)  values (?,?,?,?,?,?,?)",project.getPname(),project.getStarttime(),project.getEndtime(),project.getDid()
        ,project.getCid(),project.getUpdatetime(),project.getStatus());
        return update;
    }

    public List<Project> findAll() {
        List<Project> query
                = jdbcTemplate.query("select p.* ,d.dname from project p left join developer d on p.did=d.did where p.status=1", new BeanPropertyRowMapper<Project>(Project.class));
        for (Project project : query) {
            System.out.println(project);
        }
        return query;
    }

    public int deleteProject(int pid) {
        int update
                = jdbcTemplate.update("delete from  project where pid=?",  pid);
        int i=0;
        if (update>0){
            i=jdbcTemplate.update("update status set pid=0 where pid=?",pid);
        }
        return i;
    }

    public Project findProjectById(int pid) {
        List<Project> query
                = jdbcTemplate.query("select * from project where pid=?", new BeanPropertyRowMapper<Project>(Project.class), pid);
                    return query.get(0);

    }
}
